﻿
using System.Windows.Input;
using TestBat.IModels.Models;
using TestBat.IViewModels.Workspaces.Schedules;
using TestBat.ServiceLocator;
using TestBat.ViewModels.Attributes;
using TestBat.ViewModels.Base.Commands;
namespace TestBat.ViewModels.Workspaces.Schedules
{
    public class SchedulesViewModel : WorkspaceViewModel, ISchedulesViewModel
    {
        public SchedulesViewModel(IScheduleModel scheduleModel)
            : base()//"Schedules", new SolidColorBrush(System.Windows.Media.Color.FromRgb(255, 0, 0)))
        {
            this.scheduleModel = scheduleModel;

            this.scheduleList = null;
        }

        public IScheduleListViewModel ScheduleList
        {
            get
            {
                if (scheduleList == null)
                    scheduleList = DefaultServiceLocator.Get<IScheduleListViewModel>(); // TODO: use DI

                return scheduleList;
            }
        }

        public ICommand CreateCommand
        {
            get
            {
                return new RelayCommand(c => Create());
            }
        }

        public ICommand DeleteCommand
        {
            get
            {
                return new RelayCommand(
                c => Delete(),
                c => ScheduleList.Selected != null);
            }
        }

        [BusinessErrorDialog]
        private void Create()
        {
            scheduleModel.Create();

            scheduleList.RecreateItems();
        }

        [BusinessErrorDialog]
        private void Delete()
        {
            scheduleModel.Delete(ScheduleList.Selected.Schedule);

            scheduleList.RecreateItems();
        }

        private IScheduleListViewModel scheduleList;

        protected readonly IScheduleModel scheduleModel;
    }
}
